package com.example.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blog.common.CustomException;
import com.example.blog.common.Result;
import com.example.blog.dto.CategoryDTO;
import com.example.blog.entity.Category;
import com.example.blog.entity.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 栏目表 前端控制器
 * </p>
 *
 * @author 程序员逐梦
 * @since 2023-05-30
 */
@Api(value = "栏目相关接口", tags = "栏目相关接口")
@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController {
    
    /**
     *  查询栏目列表
     */
    @ApiOperation(value = "查询栏目列表", notes = "查询栏目列表")
    @GetMapping("/list")
    public Result<List<Category>> getUserList() {
        return new Result<List<Category>>().success(categoryService.list());
    }

    /**
     * 通过 id 查询栏目
     */
    @ApiOperation(value = "通过id查询栏目", notes = "通过id查询单个栏目")
    @GetMapping("/getbyid")
    @ApiImplicitParams({
            // name->参数名, value->参数的意义, dataType->参数类型
            @ApiImplicitParam(name = "id", value = "栏目id", dataType = "integer",
                    // paramType->参数传递类型(query string), required = true -> 表示必传参数,  defaultValue -> 参数默认值
                    paramType = "query", required = true)
    })
    public Result<Category> getById(@Param("id") Long id) {
        Result<Category> result = new Result<>();
        if (id == null || id <= 0) {
            return result.error(-1, "参数非法");
        }

        return result.success(categoryService.getById(id));
    }

    /**
     *  栏目分页查询
     */
    @ApiOperation("分页查询栏目列表")
    @PostMapping("/page")
    public Result<PageVO<Category>> getUserByPage(@RequestBody CategoryDTO categoryDTO) {
        // 查询结果降序排列
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        // id 降序排列
        wrapper.orderByDesc(Category::getId);
        // 模糊查询
        if (StringUtils.hasLength(categoryDTO.getTitle())) {
            wrapper.like(Category::getTitle, categoryDTO.getTitle());
        }

        // 设置默认页码
        if (categoryDTO.getPageNum() == null || categoryDTO.getPageNum() < 1) {
            categoryDTO.setPageNum(1);
        }
        // 设置默认每页最大条数
        if (categoryDTO.getPageSize() == null || categoryDTO.getPageSize() < 1) {
            categoryDTO.setPageSize(10);
        }

        Page<Category> page = categoryService.page(
                new Page<>(
                        categoryDTO.getPageNum() ,
                        categoryDTO.getPageSize()
                ),
                wrapper
        );
        return new Result<PageVO<Category>>().success(new PageVO<>(page));
    }

    /**
     *  新增 / 修改栏目信息
     */
    @ApiOperation(value = "数据新增或修改", notes = "通过id识别新增 or 修改操作")
    @PostMapping("/save")
    public Result<?> save(@RequestBody Category category) {
        // 参数校验
        boolean save = categoryService.saveOrUpdate(category);
        if (save) {
            return new Result<>().success();
        } else {
            throw new CustomException("操作失败!请重试,或联系管理员");
        }
    }

    /**
     *  通过id 删除栏目
     */
    @ApiOperation(value = "通过id 删除栏目", notes = "通过id批量删除栏目")
    @PostMapping("/delBatch")
    public Result<?> delBatch(@RequestBody List<Integer> ids) {
        if (ids == null || ids.size() <= 0) {
            return new Result<>().error(-1, "非法操作!");
        }
        // todo: 登录状态拦截
        boolean flag  = categoryService.removeBatchByIds(ids);
        if (flag)
            return new Result<>().success();
        else
            return new Result<>().error();
    }


}
